package com.fenda.servlet;

import com.fenda.Dao.UserDao;
import com.fenda.Util.mybatisUtil;
import com.fenda.pojo.Student;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/update")
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.sendRedirect("update.html");

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        Integer id = Integer.parseInt( req.getParameter("id"));
        int i = mapper.SelectId(id);

        if( id==i ){
            String name = req.getParameter("name");
            String age = req.getParameter("age");
            String hobby= req.getParameter("hobby");
            System.out.println(name+age+hobby);
            mapper.update(new Student(name,age,hobby));
            sqlSession.commit();
            resp.sendRedirect("index.jsp");
            System.out.println("修改成功");
        }
    }
}
